package com.tidy.seed.admin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tidy.seed.admin.system.entity.Dict;
import com.tidy.seed.admin.system.service.DictService;
import com.tidy.seed.annotation.NoToken;
import com.tidy.seed.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

// ✅ Swagger 引入
import io.swagger.annotations.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HZ-ZY
 * @since 2025-08-04
 */
@RestController
@Api(tags = "系统字典")
@RequestMapping("/admin/system/dict")
public class DictController {

    @Resource
    private DictService dictService;

    @ApiOperation(value = "新增或更新Dict")
    @PostMapping
    public Result save(@RequestBody @ApiParam("Dict 对象") Dict dict) {
        dictService.saveOrUpdate(dict);
        return Result.success();
    }

    @ApiOperation(value = "根据 ID 删除Dict")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable @ApiParam("ID") Integer id) {
        dictService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "批量删除Dict")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody @ApiParam("ID 列表") List<Integer> ids) {
        dictService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation(value = "查询全部Dict")
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") @ApiParam("父亲ID") String parentId) {
        return Result.success(dictService.list(Wrappers.<Dict>query().eq("parent_id", parentId)));
    }

    @ApiOperation(value = "根据 ID 查询Dict")
    @GetMapping("/{id}")
    public Result findOneById(@PathVariable @ApiParam("ID") Integer id) {
        return Result.success(dictService.getById(id));
    }

    @ApiOperation(value = "分页查询Dict")
    @GetMapping("/page")
    public Result findPage(@RequestParam @ApiParam("页码") Integer pageNum,
                           @RequestParam @ApiParam("页大小") Integer pageSize
    ) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(dictService.page(new Page<>(pageNum, pageSize), queryWrapper.eq("parent_id", "-1")));
    }

    @ApiOperation(value = "更具编号查询字典列表")
    @NoToken
    @GetMapping("/findDictByCode")
    public Result findAllByCode(@RequestParam @ApiParam("字典编号") String code) {
        return Result.success(dictService.list(Wrappers.<Dict>query().eq("code", code).ne("parent_id", "-1")));
    }


}
